package com.example.cardemo.widget.widget;

import com.example.cardemo.MainAbility;
import com.example.cardemo.widget.controller.FormController;
import com.example.cardemo.Http;    // 引入Http网络请求类
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormBindingData;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;

public class WidgetImpl extends FormController {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, MainAbility.class.getName());

    public WidgetImpl(Context context, String formName, Integer dimension) {
        super(context, formName, dimension);
    }

    // 网络请求的卡片数据
    private FormBindingData getCardData() {
        Http http = new Http();
        String response = http.doGet("http://api.yi-ja.com/joke/randOne");
        ZSONObject res = ZSONObject.stringToZSON(response);
        String code = res.getString("code");
        String msg = res.getString("msg");
        String content = "";
        if ("0".equals(code)) {
            ZSONObject data = res.getZSONObject("data");
            content = data.getString("content");
        } else {
            content = msg;
        }
        ZSONObject object = new ZSONObject();
        object.put("content", content);
        FormBindingData bindingData = new FormBindingData(object);
        return bindingData;
    }

    // 卡片初始化时方法
    @Override
    public ProviderFormInfo bindFormData() {
        FormBindingData bindingData = this.getCardData();
        ProviderFormInfo formInfo = new ProviderFormInfo();
        formInfo.setJsBindingData(bindingData);
        return formInfo;
    }

    // 卡片更新时方法
    @Override
    public void updateFormData(long formId, Object... vars) {

    }

    // 卡片消息事件时方法
    @Override
    public void onTriggerFormEvent(long formId, String message) {
        ZSONObject data = ZSONObject.stringToZSON(message);
        String type = data.getString("type");
        HiLog.info(TAG, "message type: " + type);
        // 刷新事件
        if ("refresh".equals(type)) {
            // 加载中
            ZSONObject object = new ZSONObject();
            object.put("content", "加载中...");
            FormBindingData bData = new FormBindingData(object);
            try {
                ((MainAbility) context).updateForm(formId, bData);
            } catch (FormException e) {
                HiLog.error(TAG, "FormException : " + e.toString());
            }
            // 接口请求
            FormBindingData bindingData = this.getCardData();
            try {
                ((MainAbility) context).updateForm(formId, bindingData);
            } catch (FormException e) {
                HiLog.error(TAG, "FormException : " + e.toString());
            }
        }
        // 其它事件
        else {

        }
    }

    @Override
    public Class<? extends AbilitySlice> getRoutePageSlice(Intent intent) {
        return null;
    }
}
